package com.example.industry.service.impl;

import com.example.industry.dao.ProjectDao;
import com.example.industry.dao.TaskDao;
import com.example.industry.dao.UserDao;
import com.example.industry.entity.Project;
import com.example.industry.entity.Task;
import com.example.industry.entity.User;
import com.example.industry.enums.Roles;
import com.example.industry.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService
{
    @Resource
    private UserDao userDao;

    @Resource
    private ProjectDao projectDao;

    @Resource
    private TaskDao taskDao;

    @Override
    public User findUserById(Integer userId)
    {
        return userDao.findById(userId);
    }

    @Override
    public User findUserByEmail(String email)
    {
        return userDao.findByEmail(email);
    }

    @Override
    public boolean register(String username, String email, String password)
    {
        User findUser = findUserByEmail(email);
        if (findUser == null)
        {
            User newUser = new User();
            newUser.setUsername(username);
            newUser.setEmail(email);
            newUser.setPassword(password);
            return userDao.addUser(newUser);
        }
        return false;
    }

    @Override
    public boolean login(String email, String password, HttpSession session)
    {
        User user = findUserByEmail(email);
        if (user != null && password.equals(user.getPassword()))
        {
            session.setAttribute("user", user);
            return true;
        }
        return false;
    }

    @Override
    public void logout(HttpSession session)
    {
        session.invalidate();
    }

    @Override
    public List<Project> findProjectsByUserIdAndRole(Integer userId, Roles role)
    {
        List<Project> projectList = new ArrayList<>();
        try
        {
            List<Integer> result = userDao.findProjectsByUserId(userId, role);
            if (result == null) return null;
            for (Integer project_id : result)
            {
                Project project = projectDao.findById(project_id);
                projectList.add(project);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return projectList;
    }

    @Override
    public List<Task> findAssignTasksByUserId(Integer userId)
    {
        List<Task> taskList  = new ArrayList<>();
        try
        {
            List<Integer> result = userDao.findAssignTasksByUserId(userId);
            if (result == null) return null;
            for (Integer task_id : result)
            {
                Task task = taskDao.findById(task_id);
                taskList.add(task);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return taskList;
    }

    @Override
    public List<Task> findAcceptTaskSByUserId(Integer userId)
    {
        List<Task> taskList  = new ArrayList<>();
        try
        {
            List<Integer> result = userDao.findAcceptTaskSByUserId(userId);
            if (result == null) return null;
            for (Integer task_id : result)
            {
                Task task = taskDao.findById(task_id);
                taskList.add(task);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return taskList;
    }
}
